<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor
        :itemList="[
       { name: 'box-base', title: '基础信息' },
      { name: 'box-dtl', title: '消耗明细' },
        ]"
      />
    </div>

    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :withRangeYMD="true" :rules="propRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <div id="box-base" class="box-title">基础信息</div>
          <customFormItem label="单号" name="busKey" :parent="_self" :form="form" disabled />

          <div class="label-class w-360">
            <strong>科室</strong>{{strings.defaultIfBlank(form.deptName,'无')}}/{{form.busUser}}
          </div>
          <customFormItem label="金额" name="amount" :parent="_self" :form="form" disabled />
          <customFormItem
            label="供应商"
            name="supplierName"
            :parent="_self"
            :form="form"
            disabled
          />
          <customFormItem label="标签管理" name="flagOmoc" :parent="_self" :form="form" :type="constCommon.formItemType.switch.name" disabled/>
          <customFormItem label="代销" name="flagConsignment" :parent="_self" :form="form" :type="constCommon.formItemType.switch.name" disabled/>
          <customFormItem label="需结算" name="flagSupplier" :parent="_self" :form="form"  :type="constCommon.formItemType.switch.name" disabled/>
          <customFormItem label="科室成本" name="flagDept" :parent="_self" :form="form"  :type="constCommon.formItemType.switch.name" disabled/>
          <customFormItem label="消耗区间" name="busDateCycle" :parent="_self" :form="form" disabled />
        </template>
      </customForm>
      <div id="box-dtl" class="box-title">消耗明细</div>
      <data-view ref= "childDataView"
        :propAutoFetchData="false"
        propViewKey="finance_bill_item_billin"
        :propCheckFlowParam="getCheckFlowParam()"
        @extra-button-click="extraButtonClick"
        :propIsTdesign="true"
      />
    </div>

      <!--仓库选择-->
      <tableDrawer
      ref="tableDrawerHigh"
      refName="tableDrawerHigh"
      viewKey="settlement_main_high"
      title="请选择"
      :subtitle="getDdChoiceUseMsg()"
      :queryWrapper="cpBillQueryWrapper"
      loadForce
      :widthPer="80"
      :heightPx="-1"
      :widthPx="-1"
    />

    <tableDrawer
      ref="tableDrawerLow"
      refName="tableDrawerLow"
      viewKey="settlement_main_low"
      title="请选择"
      :subtitle="getDdChoiceUseMsg()"
      :queryWrapper="cpBillQueryWrapper"
      loadForce
      :widthPer="80"
      :heightPx="-1"
      :widthPx="-1"
    />
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'

export default {
  data() {
    return {
      form: {},
      viewData: {}
    }
  },
  computed: {
    //弹框选择时明细查询条件
    cpBillQueryWrapper() {
      let wrapper = this.$newWrapper()
      wrapper.eq('deptIdBill', this.form.deptId)
        .eq('supplierId', this.form.supplierId)
        .eq('flagConsignment', this.form.flagConsignment)
        .eq('flagOmoc', this.form.flagOmoc)
      return wrapper
    },
  },
  created() {
    let that = this
    this.$emit('event-register', {
      validate: (callback) => {
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        this.fetchData()
      },
      afterSave: () => {  },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: async (flowButtonKey) => {
        return true
      },
      afterFlow: (valueTo, thatParam, buttonKey) => {
        if (buttonKey == 'cancelprint') { //取消打印操作
          that.fetchData()
        }
      },
      beforeClose: () => {
        return true
      },
    })
  },
  methods: {

    //加载明细
    fetchData() {
      let queryWrapper = this.$newWrapper()
      if (this.form.id) {
        queryWrapper.eq('billId', this.form.id)
      } else {
        queryWrapper.ne(1, 1)
      }
      this.$refs.childDataView.setQueryWrapper(queryWrapper)
    },

    //流程参数
    getCheckFlowParam() {
      return this.utils.getCheckFlowParam(this)
    },

    //扩展按钮
    extraButtonClick({ button, data, index, para }) {
      let {key: buttonKey} = button
      // eslint-disable-next-line default-case
      switch (buttonKey) {
      case 'cancelPrint': //取消打印
        this.cancelPrint(data)
        break
      case 'addDtl': //添加
          if (this.getPrintType() == 'high') {
            this.$refs.tableDrawerHigh.show(this.selectChangeDtl, true)
          } else {
            this.$refs.tableDrawerLow.show(this.selectChangeDtl, true)
        }
        break
      }
    },

    //取消消耗明细
    cancelPrint(rows) {
      if (rows == null || rows.length < 1) {
        this.utilMessage.warning('请选择需要操作的明细')
        return
      }
      this.$confirm('确认取消打印所选择明细，数量：' + rows.length, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let itemIds = []
        rows.forEach(row => itemIds.push(row.refBusItemId))
        let param = {
          itemIds: itemIds.join(',')
        }
        this.utilVue.loadShow(true)
        this.$ajax.post('/finance/bill/printCancel', param).then(resp => {
          if (resp.success) {
            this.utilMessage.success(resp.msg, 5000)
            this.fetchData()
            this.utils.refreshRefs(this)
          }
          this.utilVue.loadHide()
        })
      }).catch(() => {
      })
    },
    //弹框选择时用户提示信息
    getDdChoiceUseMsg() {
      return `数据筛选条件：科室:${this.form.deptName || '无'}，供应商:${this.form.supplierName || '无'}，代销:${this.form.flagConsignment ? '是' : '否'}，标签管理:${this.form.flagOmoc ? '是' : '否'}`
    },

    //弹框选择时用户提示信息
    getPrintType() {
      return this.form.flagOmoc && this.form.flagOmoc == true ? 'high' : 'low'
    },

    //选择后
    selectChangeDtl(list) {
      this.utilVue.loadShow(true)
      let itemIds = list.map(item => item.itemId)
      let paramPost = {
        itemIds: itemIds.join(','),
        printType: this.getPrintType(),
        billId: this.form.id
      }
      this.$ajax.post('/finance/bill/billPrintAddDtl', paramPost).then(resp => {
        if (resp.success) {
          this.utilMessage.success(resp.msg, 5000)
          this.fetchData()
          this.utils.refreshRefs(this)
        }
        this.utilVue.loadHide()
      })
    }
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object,
    propFlowKey: String,
    propParam: Object
  },
  components: {customAnchor},
}
</script>
